package com.yonyou.iuap.controller.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yonyou.iuap.pub.JsonUtils;
import com.yonyou.iuap.shell.entity.BdIndustryChartEntity;
import com.yonyou.iuap.shell.entity.exp.BdIndustryTreePoJo;
import com.yonyou.iuap.shell.pub.ResultDto;
import com.yonyou.iuap.shell.service.intf.IIndustryChartService;

/**
 * @author Administrator
 *	关键字选择查询
 */
@Controller
@RequestMapping("/keywordselect")
public class KeywordSelectController {
	private final Logger logger = (Logger) LoggerFactory.getLogger(getClass());
	private long start,end;
	@Autowired
	private IIndustryChartService industryChartServiceImpl;

	//通过行业查找工序
	@RequestMapping(value="/App/queryProcess",method=RequestMethod.POST)  
	public @ResponseBody String queryProcess(@RequestBody HashMap<?, ?> parMap,HttpServletRequest request,HttpServletResponse response){
		start=System.currentTimeMillis();
		ArrayList<String> list=(ArrayList<String>) parMap.get("pk_industry_chart");
		String paramString=null;
		ResultDto rsdto =new ResultDto();
		
		if(list!=null){
			for(int i=0;i<list.size();i++){
				if(i==0){
					paramString="fw_parent = "+list.get(i);
				}else{
					paramString=paramString+" or fw_parent = "+list.get(i);
				}
				//加上父节点数据
				paramString+=" or pk_industry_chart="+list.get(i);
			}
			
			logger.info("查询条件",paramString);
			rsdto=industryChartServiceImpl.selectByCondition(paramString);
			List<BdIndustryChartEntity> query=(List<BdIndustryChartEntity>) rsdto.getData();
			rsdto.setNum(query==null?0:query.size());
			//cate_type 0行业，1工序，2设备，3部件
			rsdto.setData(format(query,0));
		}else{
			rsdto.setStatus(1);
			rsdto.setMessage("参数用户为空!");
		}
		end=System.currentTimeMillis();
		logger.info("queryProcess接口调用时间"+String.valueOf(end-start));
		return JsonUtils.toJsonStr(rsdto);
	}

	//通过工序查找设备
	@RequestMapping(value="/App/queryEquipment",method=RequestMethod.POST)  
	public @ResponseBody String queryEquipment(@RequestBody HashMap<?, ?> parMap,HttpServletRequest request,HttpServletResponse response){
		start=System.currentTimeMillis();
		ArrayList<String> list=(ArrayList<String>) parMap.get("pk_industry_chart");
		String paramString=null;
		ResultDto rsdto =new ResultDto();
		
		if(list!=null){
			for(int i=0;i<list.size();i++){
				if(i==0){
					paramString="fw_parent = "+list.get(i);
				}else{
					paramString=paramString+" or fw_parent = "+list.get(i);
				}
				//加上父节点数据
				paramString+=" or pk_industry_chart="+list.get(i);
			}
			
			logger.info("查询条件",paramString);
			rsdto=industryChartServiceImpl.selectByCondition(paramString);
			List<BdIndustryChartEntity> query=(List<BdIndustryChartEntity>) rsdto.getData();
			rsdto.setNum(query==null?0:query.size());
			rsdto.setData(format(query,1));
		}else{
			rsdto.setStatus(1);
			rsdto.setMessage("参数用户为空!");
		}
		end=System.currentTimeMillis();
		logger.info("queryEquipment接口调用时间"+String.valueOf(end-start));
		return JsonUtils.toJsonStr(rsdto);
	}
	
	/**
	 * @param query
	 * @return
	 * 格式化返回参数
	 */
	private List<BdIndustryTreePoJo> format(List<BdIndustryChartEntity> query,int father_type) {
		
		List<BdIndustryTreePoJo> fList=new ArrayList<BdIndustryTreePoJo>();
		List<BdIndustryTreePoJo> sList=new ArrayList<BdIndustryTreePoJo>();
		
		
		
		if(query!=null && query.size()>0){
			for(BdIndustryChartEntity bean:query){
				if(bean.getCate_type()==father_type){
					BdIndustryTreePoJo fBean=new BdIndustryTreePoJo();
					fBean.setCate_code(bean.getCate_code());
					fBean.setCate_name(bean.getCate_name());
					fBean.setCate_type(father_type);
					fBean.setFw_parent(bean.getFw_parent());
					fBean.setPk_industry_chart(bean.getPk_industry_chart());
					fList.add(fBean);
				}else {
					BdIndustryTreePoJo sBean=new BdIndustryTreePoJo();
					sBean.setCate_code(bean.getCate_code());
					sBean.setCate_name(bean.getCate_name());
					sBean.setCate_type(father_type);
					sBean.setFw_parent(bean.getFw_parent());
					sBean.setPk_industry_chart(bean.getPk_industry_chart());
					sList.add(sBean);
				}
			}
			
			for(BdIndustryTreePoJo f:fList){
				List<BdIndustryTreePoJo> temp=new ArrayList<BdIndustryTreePoJo>();
				for(BdIndustryTreePoJo s:sList){
					if(f.getPk_industry_chart().equals(s.getFw_parent())){
						temp.add(s);
					}
				}
				f.setData(temp);
			}
		}
		
		return fList;
	}

	//通过行业选择工序、设备及选中状态
	@RequestMapping(value="/queryHyGxSb",method=RequestMethod.POST)
	public @ResponseBody String queryHyGxSb(@RequestBody HashMap<String, Object> parMap,HttpServletRequest request,HttpServletResponse response){
		start=System.currentTimeMillis();
		//传入数组转成字符串
		ArrayList<String> list=(ArrayList<String>) parMap.get("wherein");
		parMap.put("wherein", StringUtils.join(list.toArray(), ","));
		ResultDto rsdto = industryChartServiceImpl.queryHyGxSb(parMap);
		end=System.currentTimeMillis();
		logger.info("queryHyGxSb接口调用时间"+String.valueOf(end-start));
		return JsonUtils.toJsonStr(rsdto);
	}
	
}
